# usage:
# - SVN DIR
# shell>  python replay.py <replay-file.gz> [f/false]
# - REPLAY DIR
# shell>  python ../replay.py <replay-file.gz> [f/false]
# - SUB-REPLAY DIR
# shell>  python ../../replay.py <replay-file.gz> [f/false]
#
# [f / false] = optional, if added, rendered=False
#

# import Domination directory
import os, sys
path, filename = os.path.split("../Domination-Game/domination")
sys.path.append(path)
path, filename = os.path.split("../../Domination-Game/domination")      #enabling to run from replay-dir
sys.path.append(path)
path, filename = os.path.split("../../../Domination-Game/domination")   #enabling to run from replay-dir
sys.path.append(path)    

# import statements
import cPickle as pickle
import gzip
from domination import core

def getTeamName(number):
    team = "Team " + str(number);
    if (number == "1"):
        team += " Emire"
    elif (number == "2"):
        team += " Eschew Obfuscation"
    elif (number == "3"):
        team += " Excrucians"
    elif (number == "4"):
        team += " Stormtroopers"
    elif (number == "5"):
        team += " Cereal Killers"
    elif (number == "6"):
        team += " Cyno"
    return team
    
    
if (len(sys.argv) > 1):
    replayfile = sys.argv[1]
    
    rendered = True
    if (len(sys.argv) > 2):
        if (sys.argv[2] == "f") or (sys.argv[2] == "false"):
            rendered = False

    # open file
    file = gzip.open(replayfile)
    content = file.read();
    replay = pickle.loads(content)

    #print settings
    s = replayfile.split('_')
    red = s[1]
    blue = s[3].split('.')[0]

    teamRed = getTeamName(red[1])
    teamRed += " - " + replay.red_name + " (" + red[2:4] + ")"
    teamBlue = getTeamName(blue[1])
    teamBlue += " - " + replay.blue_name + " (" + blue[2:4] + ")"
    
    print "---------------------------\n"
    print "RED : " + teamRed
    print "BLUE: " + teamBlue + "\n" 
    print "---------------------------"

    # replay!
    playback = core.Game(replay=replay, rendered=rendered)
    playback.run()
else:
    print "Please provide a replay file"

    
    
    

    